package com.internetCafes.spms.web.extend.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.internetCafes.spms.common.base.BaseWebController;
import com.internetCafes.spms.core.annotation.SysLog;
import com.internetCafes.spms.core.utils.DateUtils;
import com.internetCafes.spms.core.utils.R;
import com.internetCafes.spms.web.extend.model.EntCertiQuali;
import com.internetCafes.spms.web.extend.model.EntCertiQualiRenewal;
import com.internetCafes.spms.web.extend.service.IEntCertiQualiRenewalService;
import com.internetCafes.spms.web.extend.service.IEntCertiQualiService;
import com.internetCafes.spms.web.extend.vo.EntCertiQualiRenewalVo;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Date;
import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author LinYe
 * @since 2020-12-08
 */
@RestController
@RequestMapping("/cert/entQualiRenewal")
public class EntCertiQualiRenewalController extends BaseWebController {

    @Autowired
    IEntCertiQualiRenewalService entCertiQualiRenewalService;

    @Autowired
    IEntCertiQualiService entCertiQualiService;

    @SysLog("保存资质证书延期记录")
    @PostMapping("/save")
    @RequiresPermissions("certificate:certificateInfo:save")
    public R save(@RequestBody EntCertiQualiRenewal entity) {
        entCertiQualiRenewalService.save(entity);
        EntCertiQuali entCertiQuali = new EntCertiQuali();
        entCertiQuali.setId(entity.getCertificateId());
        entCertiQuali.setExpireDate(entity.getNewExpireDate());
        entCertiQualiService.updateById(entCertiQuali);

        return R.ok();
    }

    @SysLog("作废资质证书延期记录")
    @PostMapping("/delete/{id}")
    @RequiresPermissions("certificate:certificateInfo:delete")
    public R delete(@PathVariable("id") Long id) {
        if (entCertiQualiRenewalService.removeById(id)) {
            return R.ok();
        } else {
            return R.error("您要删除的证书不存在");
        }
    }

    @SysLog("更新资质证书延期记录")
    @PostMapping("/update")
    @RequiresPermissions("certificate:certificateInfo:update")
    public R update(@RequestBody EntCertiQualiRenewal entity) {
        if (entCertiQualiRenewalService.updateById(entity)) {
            EntCertiQuali entCertiQuali = new EntCertiQuali();
            entCertiQuali.setId(entity.getCertificateId());
            entCertiQuali.setExpireDate(entity.getNewExpireDate());
            entCertiQualiService.updateById(entCertiQuali);
            return R.ok();
        } else {
            return R.error("您要更新的证书不存在");
        }
    }

    @GetMapping("/getDetail/{id}")
    @RequiresPermissions("certificate:certificateInfo:info")
    public R get(@PathVariable("id") Long id) {
        EntCertiQualiRenewal entCertiQualiRenewal = entCertiQualiRenewalService.getById(id);
        if (entCertiQualiRenewal != null) {
            return R.ok().put("data", entCertiQualiRenewal);
        } else {
            return R.error("您要查询的证书不存在");
        }
    }

    @PostMapping("/paging")
    @RequiresPermissions("certificate:certificateInfo:list")
    public R paging(@RequestBody EntCertiQualiRenewalVo entCertiQualiRenewalVo) {
        Page<EntCertiQualiRenewal> page=new Page<>(entCertiQualiRenewalVo.getPageNum(),entCertiQualiRenewalVo.getPageSize());
        QueryWrapper<EntCertiQualiRenewal> entCertiQualiRenewalWrapper = new QueryWrapper<>(entCertiQualiRenewalVo);
        if (entCertiQualiRenewalVo.getOrderBy()!=null) {
            entCertiQualiRenewalWrapper.orderBy(entCertiQualiRenewalVo.getOrderBy() != null, entCertiQualiRenewalVo.isAscFlag(), com.internetCafes.spms.common.utils.StringUtils.HumpToUnderline(entCertiQualiRenewalVo.getOrderBy()));
        }
        entCertiQualiRenewalWrapper.orderBy(true,false,"id");
        if (entCertiQualiRenewalVo.getValidityType()!=null) {
            switch (entCertiQualiRenewalVo.getValidityType()) {
                case -1:
                    entCertiQualiRenewalWrapper.lt("new_expire_date", new Date());
                    break;
                case 0:
                    entCertiQualiRenewalWrapper.gt("new_expire_date", new Date());
                    entCertiQualiRenewalWrapper.lt("new_expire_date", entCertiQualiRenewalVo.getDays() != null && entCertiQualiRenewalVo.getDays() != "" ? DateUtils.getBeAfDay(Integer.parseInt(entCertiQualiRenewalVo.getDays())) : entCertiQualiRenewalVo.getMonths() != null && entCertiQualiRenewalVo.getMonths() != "" ? DateUtils.getBeAfMonth(Integer.parseInt(entCertiQualiRenewalVo.getMonths())) : null);
                    break;
                case 2:
                    entCertiQualiRenewalWrapper.lt("new_expire_date", new Date());
                    entCertiQualiRenewalWrapper.or(wrapper -> wrapper.gt("new_expire_date", new Date()).lt("new_expire_date", entCertiQualiRenewalVo.getDays() != null ? DateUtils.getBeAfDay(Integer.parseInt(entCertiQualiRenewalVo.getDays())) : entCertiQualiRenewalVo.getMonths() != null ? DateUtils.getBeAfMonth(Integer.parseInt(entCertiQualiRenewalVo.getMonths())) : null));
                    break;
                default:
            }
        }
        IPage<EntCertiQualiRenewal> entCertiQualiRenewalIPage = entCertiQualiRenewalService.page(page,entCertiQualiRenewalWrapper);
        return R.ok().put("data", entCertiQualiRenewalIPage);
    }

    @PostMapping("/list")
    @RequiresPermissions("certificate:certificateInfo:list")
    public R list(@RequestBody EntCertiQualiRenewalVo entCertiQualiRenewalVo) {
        QueryWrapper<EntCertiQualiRenewal> entCertiQualiRenewalQueryWrapper = new QueryWrapper<>(entCertiQualiRenewalVo);
        entCertiQualiRenewalQueryWrapper.orderBy(entCertiQualiRenewalVo.getOrderBy()!=null,entCertiQualiRenewalVo.isAscFlag(),entCertiQualiRenewalVo.getOrderBy());
        List<EntCertiQualiRenewal> list = entCertiQualiRenewalService.list(entCertiQualiRenewalQueryWrapper);
        return R.ok().put("data", list);
    }

    @PostMapping("/count")
    @RequiresPermissions("certificate:certificateInfo:list")
    public R count(@ModelAttribute EntCertiQualiRenewal entCertiQualiRenewal) {
        QueryWrapper<EntCertiQualiRenewal> entCertiQualiRenewalQueryWrapper = new QueryWrapper<>(entCertiQualiRenewal);
        int count = entCertiQualiRenewalService.count(entCertiQualiRenewalQueryWrapper);
        return R.ok().put("count", count);
    }
}
